<template>
  <el-dialog :append-to-body="true" :visible.sync="visible" v-dialogDrag :title="title" :close-on-click-modal="false" class="tl-dialog page" width="1200px" @open="open" @close="close">
  <div class="tl-form-line-title">进度反馈</div>
    <el-form class="tl-form stakeholderForm" ref="form" :model="model" :rules="rules" label-width="300px">
		<el-row>
			<el-col :span="24">
				<el-form-item label="进度更新" prop="completion" :rules="rules.validateRequire">
					<el-input v-model="model.completion" type="textarea" show-word-limit maxlength="1000" :rows="3" :disabled="!isAdd"></el-input>
				</el-form-item>
			</el-col>
		</el-row>
		<el-row>
      <el-col :span="12">
				<el-form-item label="完成得分" prop="completionscore" :rules="rules.validateRequire">
					<el-input v-model="model.completionscore" @change="changescore" oninput="this.value=this.value.match(/\d{0,}\.?\d{0,1}/)" :disabled="!isAdd"></el-input>
					<span class="unitSpan">分</span>
				</el-form-item>
			</el-col>
			<el-col :span="12">
				<el-form-item label="本周提升分数" prop="improvescore" :rules="rules.validateRequire">
					<el-input v-model="model.improvescore" :disabled="true"></el-input>
					<span class="unitSpan">分</span>
				</el-form-item>
			</el-col>
		</el-row>
    <el-row>
        <el-col :span="12">
          <el-form-item label="更新日期">
            <el-date-picker v-model="changetime" type="date" value-format="yyyy-MM-dd" :disabled="true"></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="12">
				<el-form-item label="是否完成" prop="isfinishbidscore" :rules="rules.validateRequire">
						<el-select v-model="model.isfinishbidscore" filterable clearable :disabled="!isAdd">
							<el-option v-for="item in codeModel.isfinishbidscoreList" :key="item.code" :label="item.name" :value="item.code" />
						</el-select>
				</el-form-item>
			</el-col>
      </el-row>
    </el-form>
    <div style="float:right">
      <el-button type="primary" @click="$refs.history.visible = true" v-if="!isAdd"><svg-icon icon-class="list" />审批历史</el-button>
      <el-button :type="!isAdd === false ? 'text' : 'primary'" @click="doCancelEdit">取消</el-button>
      <el-button type="primary" @click="dosave" :loading="saveLoading" v-if="isAdd"><svg-icon icon-class="send" />提交</el-button>
    </div>
    <div v-if="feedbackList && feedbackList.length > 0" class="tl-form-line-title">历史反馈</div>
    <feed-back-his ref="feedbackhisref" :feedbackList="feedbackList"></feed-back-his>
    <tl-approval-history ref="history" sourceType="MpmProjectBidScoreFeedBack" :sourceId="model.id" />
    <div slot="footer" class="dialog-footer"></div>
  </el-dialog>
</template>

<script>
/** mixins */
import { mixin_approval_edit } from '@tapui/mixins/mixin_approval_edit'
/** 子组件 */
import TlRefStakeholder from '../mpmProjectStakeholder/refList'
import FeedBackHis from './feedBackHis'
/** 工具类 */
import { putAction, getAction, postAction, deleteAction } from '@tapui/utils/http'
/** VUE配置 */
export default {
  mixins: [mixin_approval_edit],
  components: { TlRefStakeholder, FeedBackHis },
  data() {
    return {
      moduleTitle: '自评反馈',
      urlPrefix: '/mpm/projectBidScoreFeedBack/',
      url: {
        // 额外的url
        queryCodeList: '/mpm/projectBidScoreFeedBack/dataCodeList',
        queryScoreFeedBackHisInfo: '/mpm/projectBidScoreFeedBack/queryScoreFeedBackHisInfo'
      },
      externalQueryParam: {
        qry_projectcode: this.projectinfo.projectcode // 项目编号
      },
      pickerOptions: {
        disabledDate: time => {
            return time.getTime() > this.biddate.getTime()
        }
      },
      title: '自评反馈',
      type: '1',
      times: '1',
      bidScoreInfo: {},
      changetime: new Date()
    }
  },
  props: {
    projectinfo: {},
    feedbackList: [],
    biddate: {
      type: Date,
      default: new Date()
    }
  },
  methods: {
    assignDefaultValue() {
      if (this.isAdd) {
        if (this.codeModel.flowidList && this.codeModel.flowidList.length > 0) {
          this.$set(this.model, 'flowid', parseInt(this.codeModel.flowidList[0].id))
        }
        this.$set(this.model, 'sourceid', this.bidScoreInfo.id)
        this.$set(this.model, 'type', this.bidScoreInfo.type)
        this.$set(this.model, 'times', this.bidScoreInfo.times)
        this.$set(this.model, 'projectcode', this.bidScoreInfo.projectcode)
        this.$set(this.model, 'responsiblecode', this.bidScoreInfo.responsiblecode)
        this.$set(this.model, 'responsible', this.bidScoreInfo.responsible)
        this.$set(this.model, 'orgcode', this.org.code)
        this.$set(this.model, 'disporgcode', this.org.name)
        this.$set(this.model, 'deptcode', this.dept.code)
        this.$set(this.model, 'deptname', this.dept.fullname)
        this.$set(this.model, 'usercode', this.user.code)
        this.$set(this.model, 'username', this.user.name)
        this.$set(this.model, 'isfinishbidscore', '0')
      }
    },
    loaded() {
      // 页面加载完毕执行
      if (!this.isAdd && this.model.docno) {
        this.title = this.title + `（单据编号：${this.model.docno}）`
      }
      // if (this.isAdd) {
      //   this.disableEditSubmit === false
      // } else {
      //   this.disableEditSubmit === true
      // }
    },
    open() {
      this.model = {}
      this.$nextTick(() => {
        this.$refs.form.clearValidate()
        if (this.isAdd === true) {
          this.title = '新增' + this.moduleTitle
          this.assignDefaultValue()
        } else {
          this.title = '查看' + this.moduleTitle
          this.queryData(this.id).then((_) => {
            this.loaded()
          })
        }
      })
    },
    beforeSubmit() {
      this.$set(this.model, 'notsavehis', true)
      return true
    },
    /** 提交审批 */
    doSubmitApproval() {
      if (!this.url.submit) {
        this.$tap.alert('请设置审批地址!', 'warning')
        return
      }
      this.submitLoading = true
      if (!this.beforeApproval()) {
        this.submitLoading = false
        return
      }
      putAction(this.url.submit, this.model).then((res) => {
        if (res.success) {
          this.$tap.message(res.message, 'success')
          this.queryData(this.model.id)
          this.afterApproval()
        } else {
          this.$tap.alert(res.message, 'warning')
        }
        this.submitLoading = false
      })
    },
    async dosave() {
      if (!this.model.completion || (!this.model.improvescore) || !this.model.completionscore) {
        this.$tap.alert('请填写必填信息！')
        return
      }
      let savetemp = await this.addBase()
      if (savetemp) {
        this.doSubmitApproval()
        this.visible = false
        this.$emit('doRefresh')
      }
    },
    async addBase() {
      if (this.beforeSubmit()) {
        this.saveLoading = true
        if (this.isAdd) {
          return await postAction(this.url.add, this.model)
            .then(async res => {
              if (res.success) {
                return await this.queryData(res.result.id).then(_ => {
                  this.isAdd = false
                  this.afterSubmit()
                  this.saveLoading = false
                  return true
                })
              } else {
                this.$tap.alert(res.message, 'warning')
                this.afterSubmit()
                this.saveLoading = false
                return false
              }
            })
            .catch(err => {
              this.saveLoading = false
              return false
            })
        }
      }
    },
	changescore() {
    // 分差
    let scoregap = parseFloat(this.bidScoreInfo.scoregap ? this.bidScoreInfo.scoregap : 0)
    // 自评分
    let selfscore = parseFloat(this.bidScoreInfo.selfscore ? this.bidScoreInfo.selfscore : 0)
    // 满分
		let fullscore = parseFloat(this.bidScoreInfo.fullscore ? this.bidScoreInfo.fullscore : 0)
		let completionscore = parseFloat(this.model.completionscore ? this.model.completionscore : 0)
		if (fullscore < completionscore) {
			this.$tap.alert('完成得分不能大于满分！')
      this.$set(this.model, 'completionscore', '0')
      this.$set(this.model, 'improvescore', -selfscore + '')
			return
		}
    // 本周提升分数=完成得分-自评分  同时需满足<=分差
    let improvescore = (completionscore - selfscore + '').indexOf('.') > 0 ? Number(completionscore - selfscore).toFixed(1) : Number(completionscore - selfscore)
    this.$set(this.model, 'improvescore', improvescore + '')
	}
  }
}
</script>
<style scoped lang="scss">
.stakeholderForm {
  margin-top: 15px;
  margin-left: -220px;
  .el-row {
    margin-bottom: 2px;
  }
  /* 隐藏禁用状态下的提示 */
  .is-disabled ::placeholder {
    color: transparent !important;
  }
}
.tl-ref-user {
  /deep/ .tl-ref-selector {
    .el-button {
      background-color: red !important;
    }
  }
}
</style>
